﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Core;
using SharedGenomics.Domain;
using SharedGenomics.DataTransfer;

namespace SharedGenomics.Workbench.PresentationModel
{
    public class FilterParametersPTO : PresentationTransferObject
    {
        //Missingness per individual
        public bool ApplyMindFilter
        {
            get { return this.GetPropertyValue<bool>("ApplyMindFilter"); }
            set { this.SetPropertyValue("ApplyMindFilter", value); }
        }

        //Missingness rate per SNP in the population (AKA call rate)
        public bool ApplyGenoFilter
        {
            get { return this.GetPropertyValue<bool>("ApplyGenoFilter"); }
            set { this.SetPropertyValue("ApplyGenoFilter", value); }
        }

        public bool ApplyHardyWeinbergFilter
        {
            get { return this.GetPropertyValue<bool>("ApplyHardyWeinbergFilter"); }
            set { this.SetPropertyValue("ApplyHardyWeinbergFilter", value); }
        }

        public string MinIndividualMissingess
        {
            get { return this.GetPropertyValue<string>("MinIndividualMissingess"); }
            set { this.SetPropertyValue("MinIndividualMissingess", value); }
        }

        public string MinGenotypingRate
        {
            get { return this.GetPropertyValue<string>("MinGenotypingRate"); }
            set { this.SetPropertyValue("MinGenotypingRate", value); }
        }

        public string MinMAF
        {
            get { return this.GetPropertyValue<string>("MinMAF"); }
            set { this.SetPropertyValue("MinMAF", value); }
        }

        public string MinHardyWeinbergP
        {
            get { return this.GetPropertyValue<string>("MinHardyWeinbergP"); }
            set { this.SetPropertyValue("MinHardyWeinbergP", value); }
        }

        public FileSetDataDistributionSummary DataDistributionSummary
        {
            get { return this.GetPropertyValue<FileSetDataDistributionSummary>("DataDistributionSummary"); }
            set { this.SetPropertyValue("DataDistributionSummary", value); }
        }

        public IEnumerable<ValueDisplayItem<string>> PhenotypeColumns
        {
            get { return this.GetPropertyValue<IEnumerable<ValueDisplayItem<string>>>("PhenotypeColumns"); }
            set { this.SetPropertyValue("PhenotypeColumns", value); }
        }

        public string SelectedPhenotypeColumn
        {
            get { return this.GetPropertyValue<string>("SelectedPhenotypeColumn"); }
            set { this.SetPropertyValue("SelectedPhenotypeColumn", value); }
        }

        protected override void Validate(IValidationErrorCollection validationErrors)
        {
            if (this.ApplyGenoFilter && !this.IsFilterValueValid(this.MinGenotypingRate))
                validationErrors.Add("Missingness must be in the range [0, 1]", this, "MinGenotypingRate");

            if (this.ApplyMindFilter && !this.IsFilterValueValid(this.MinIndividualMissingess))
                validationErrors.Add("Missingess must be in the range [0, 1]", this, "MinIndividualMissingess");

            if (!this.IsFilterValueValid(this.MinMAF))
                validationErrors.Add("MAF must be in the range [0, 1]", this, "MinMAF");

            if (!this.ApplyHardyWeinbergFilter && !this.IsFilterValueValid(this.MinHardyWeinbergP))
                validationErrors.Add("HWE must be in the range [0, 1]", this, "MinHardyWeinbergP");

            if (this.SelectedPhenotypeColumn.IsEmptyOrWhitespace())
                validationErrors.Add("Phenotype column required", this, "SelectedPhenotypeColumn");
        }

        private bool IsFilterValueValid(string value)
        {
            decimal d;
            return decimal.TryParse(value, out d) && d.IsBetween(0.0M, 1.0M, true);
        }
    }
}
